<?php
/**
 * Fastapp.
 * 5/11/24
 * @Author deepsea
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp;

use Hyperf\Framework\Bootstrap\ServerStartCallback;

class FastAppStart extends ServerStartCallback
{
    public function beforeStart(): void
    {
        $console = console();
        $console->info('Fastapp start success...');
        $console->info($this->welcome());
        $this->initStart();
        str_contains(PHP_OS, 'CYGWIN')
        && $console->info('current booting the user: ' . shell_exec('whoami'));
    }

    protected function welcome(): string
    {
        $welcome = '
----------------- welcome to use Fastapp -----------------
_____________  Copyright Fastapp 2024 ~ %y  _____________
          ';


        return str_replace([
            '%y',
        ], [
            date('Y'),
        ], $welcome);
    }

    protected function initStart(): void
    {
        set_currency_cache();
        $dir = BASE_PATH . '/app';
        $dh = opendir($dir);
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != ".." && file_exists($dir . '/' . $file . '/Start.php')) {
                require $dir . '/' . $file . '/Start.php';
            }
        }
        closedir($dh);
    }
}